package br.com.LeitorLog.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

import br.com.LeitorLog.basicas.Item;
import br.com.LeitorLog.controladores.ControladorUtil;
import br.com.LeitorLog.gui.util.ConstantesGUI;
import br.com.LeitorLog.gui.util.Tabela;

/**
 * PAINELTESTE
 *
 * @since 30/01/2013 10:22:37
 */
public class PainelItem extends JPanel implements ActionListener {

	private static final long serialVersionUID = 2318095425646156126L;
	private JTextField atextField;
	private String path = "";
	private JFileChooser chooser;
	
	private	Tabela		table;
//	private ItemTableModel myModel;
	private DefaultTableModel model = new DefaultTableModel(); 

	/**
	 * Create the panel.
	 */
	public PainelItem() {
		
		JPanel panel = new JPanel();
		
		JScrollPane scrollPane = new JScrollPane();
		
		popularGrid();
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
					.addContainerGap())
		);	

		scrollPane.setViewportView(table);
		
		JLabel label = new JLabel(ConstantesGUI.Mensagens.INFORME_ARQUIVO);
		
		atextField = new JTextField();
		atextField.setEditable(false);
		atextField.setColumns(22);
		
		JButton button = new JButton(ConstantesGUI.Mensagens.SELECIONAR);
		button.addActionListener(this);
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(label, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE)
					.addGap(5)
					.addComponent(atextField, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
					.addGap(5)
					.addComponent(button, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(18, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(4)
							.addComponent(label))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(1)
							.addComponent(atextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(button))
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		setLayout(groupLayout);

	}
	
	/**
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent pE) {
		//System.out.println("AAA");
		atextField.setText("");
		chooser = new JFileChooser();
		// Filtro para selecionar apenas arquivos texto
		FileFilter filter = new FileNameExtensionFilter("Arquivo Log", "log");    
		chooser.addChoosableFileFilter(filter);
		// desabilitar a opcao de all types 
		chooser.setAcceptAllFileFilterUsed(false);
		
		int escolha = chooser.showOpenDialog(null);
	   	
		// verifica qual opção foi selecionada na escolha do arquivo de log.
		if (escolha == JFileChooser.APPROVE_OPTION) {
			
			// Reseta o grid
			model = (DefaultTableModel) table.getModel();
		    model.setRowCount(0);
			
			//System.out.println("Aprovado");
			path = chooser.getSelectedFile().getPath();
			atextField.setText(path);
			ControladorUtil controladorUtil = new ControladorUtil();
			// processando arquivo e setando no text area
			//textArea.setText(controladorItem.processarArquivoItens(path));
			this.popularValores(controladorUtil.processarArquivoItens(path));
		} else if (escolha == JFileChooser.CANCEL_OPTION) {
			//System.out.println("Cancelado");			
		}
	}
	
	public void popularValores(ArrayList<Item> listaItens) {
		
		for (Item vItem : listaItens) {
			String data = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss").format(vItem.getDataInclusao());
			String descricao = vItem.getDescricao();
			String isLeg = vItem.isLegendario()?ConstantesGUI.Mensagens.SIM:ConstantesGUI.Mensagens.NAO;
			String tipo = vItem.getTipo();
			String score = vItem.getScore() + "";
			
			model.addRow(new Object[] {data, descricao, isLeg, tipo, score});
		}
	}

	public void popularGrid() {
//		ArrayList<Item> lista = new ArrayList<Item>();
//		String[] headers = {"Data", "Descricao", "É legendário?", "Tipo", "Score"};
//		myModel = new ItemTableModel(lista, headers);
		
		
		model.addColumn(ConstantesGUI.Mensagens.COLUNA_DATA);
		model.addColumn(ConstantesGUI.Mensagens.COLUNA_DESCRICAO);
		model.addColumn(ConstantesGUI.Mensagens.COLUNA_LEGENDARIO);
		model.addColumn(ConstantesGUI.Mensagens.COLUNA_TIPO);
		model.addColumn(ConstantesGUI.Mensagens.COLUNA_SCORE);
		
		table = new Tabela(model);
//		table = new JTable(dataValues, headers);
	}
}
